using System;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.Sqlite;
using Volo.Abp.Modularity;
using Volo.Abp.Uow;
using Matrix.ServiceDiscovery.EntityFrameworkCore;

namespace Matrix.ServiceDiscovery
{
    [DependsOn(
        typeof(MatrixServiceDiscoveryDomainModule),
        typeof(MatrixServiceDiscoveryApplicationModule),
        typeof(AbpEntityFrameworkCoreSqliteModule)
    )]
    public class MatrixServiceDiscoveryTestModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            context.Services.AddEntityFrameworkInMemoryDatabase();

            Configure<AbpDbContextOptions>(options =>
            {
                options.Configure(abpDbContextConfigurationContext =>
                {
                    abpDbContextConfigurationContext.DbContextOptions.UseSqlite(
                        CreateInMemoryDatabase()
                    );
                });
            });

            // 配置测试用数据库
            Configure<AbpUnitOfWorkDefaultOptions>(options =>
            {
                options.TransactionBehavior = UnitOfWorkTransactionBehavior.Disabled;
            });
        }

        private static string CreateInMemoryDatabase()
        {
            var connectionString = $"Data Source=InMemory_{Guid.NewGuid():N};Mode=Memory;Cache=Shared";
            return connectionString;
        }
    }
}